1. Field of the Invention
The present invention relates to a compile method and a method for tracing a stack.
2. Background of the Invention
Presently, Java(trademark) is widely used, not only as a common language for network computing but also as a standard language of object-oriented languages not dependent on any platform. A program written in Java(trademark) is translated into a bytecode which is executed by a Java(trademark) viitual machine. Accordingly, a program once written in Java(trademark) has an advantage that is operates on any computer supporting Java(trademark) regardless of its CPU (multi-platform).
Despite these advantages, execution of a bytecode by a virtual machine is inferior to direct execution of a machine language code in terms of execution performance. Thus, in general, the bytecode is translated into a machine language code (hereafter, referred to as xe2x80x9cJITed codexe2x80x9d) by a JIT compiler during operation of a Java(trademark) program so as to execute a JITed code instead of a bytecode. The minimum unit of compilation is a subroutine called a method. A frequently executed code is translated into a machine language so that performance of a machine language code may be exploited while preserving multi-platform characteristics of a Java(trademark) bytecode.
A JITed code for a method is equal to an optimization code generated by C compiler and so on the point of being a code suited to a CPU on which a program operates. In general, in the currently popular CPUS, if a subroutine is invoked, the subroutine forms on a stack an area for storing a local variable to be used by itself (referred to as a xe2x80x9cframexe2x80x9d). FIG. 1 shows a state of a stack when subroutine A has invoked subroutine B and subroutine B has invoked subroutine C. In this drawing, the stack extends upward. The areas represent respective frames of the subroutines. Stack pointer SP points to the top of this stack, namely the top of frame C. If subroutine C further invokes subroutine D, a frame of subroutine D is newly formed from the current SP and the SP points to the top of the frame again. When returning from each subroutine, its frame is removed. For instance, in returning from subroutine D to subroutine C, the frame of subroutine D is removed before returning and the SP returns to the value when subroutine C invoked subroutine D and points to the top of frame C again. In the case of Java(trademark), CPU resources are allocated to each executable unit called a thread which has its own stack (referred to as a xe2x80x9cthread stackxe2x80x9d). A JITed code creates the above-mentioned frame (hereafter referred to as xe2x80x9cJITed framexe2x80x9d) on a thread stack.
It is not only a JITed frame that is in a thread stack. A JITed code invokes various service routines provided by a Java(trademark) virtual machine. There are also service routines which further activate a new Java(trademark) virtual machine. FIG. 2 shows a state in which frames of a JITed code and other codes are mixed in a thread stack. The portions with alphabetic characters are JITed frames.
A base pointer is usually used for tracing a stack. For instance, base pointer EBP of x86 (herein EBP) always holds a base address of a current frame in a stack to facilitate detection of a frame in a stack. For instance, in the case of debugging a C program and the like, it is helpful to know invocation relationship of a method by tracing a stack. A frame chain by EBP is often used for this purpose.
As a general compiler optimization technique, particularly an important one for an x86 architecture, which has a smaller number of registers, there is omission of a base pointer. In the above-mentioned example of a C program, when debugging has ended and a final code is to be built, optimization is performed by releasing EBP from a frame chain so that maintenance overhead of the frame chain decreases to allow an EBP register to be allocated as a general register. Thus, a fast code may be generated. This optimization is possible in a language like C, certainly because it is not necessary to trace a stack at a runtime.
In Java(trademark) JIT compiler, such omission of a base pointer is difficult. It is because, in the language specifications of Java(trademark), classLoaderDepth( ) of Java.lang.SecurityManager, countStackFrames( ) of Java.lang.Thread and the like require a function equal to tracing a stack. In addition, there is also necessity of storing in an exception object a stack trace of the time when an exception occurs. These do not function if a base pointer is omitted.
Apart from the above, there is also a cause in a mechanism of JITed code discarding which makes omission of a base pointer difficult. There are cases where, if the memory used by a JIT compiler including a JiTed code is limited or to be limited, only class unload is not sufficient. For instance, in a desktop environment where operation is on a thin client without any virtual memory by a hard disk such as a network computing machine (hereafter, an xe2x80x9cNC machinexe2x80x9d), multiple Java(trademark) applications such as a mail, a word processor, a scheduler, and the like are simultaneously activated and thousands of methods may be invoked, though a significant percentage of those methods are not present (are not active) in each thread stack. Since class unload only discards a class and its method with no possibility of being used in a Java(trademark) system, a JITed code of such a nonactive method is not discarded. Thus, a JIT compiler has a mechanism of JITed code discarding, which locates an active method in any thread stack and discards a nonactive method (or part of it) to set memory free. To discover an active method correctly and fast, a frame chain to couple only JITed code frames is indispensable.
An object of the present invention is to enable a trace of a stack even in a state where the base pointer is omitted.
At a compilation time in the present invention, a process is performed: for storing into a storage during a compilation of a method, pairs of an address of a code and information concerning a size of a stack frame after a stack pointer is moved, the code causing an address of the stack pointer to be changed; and for registering a table which is constituted by the pairs and an initial frame size with correspondence to the compiled method. While information of a stack frame size after change is suitably the amount of change in the stack pointer from the initial frame size, it may be the stack frame size itself after the change. In the former case, the table includes the initial frame size, or the initial frame size is provided separately.
If a pause is taken and a trace is performed when such a process is performed at a compilation time and a JITed code is executed in a suitable form, the steps are taken for: obtaining and storing into a storage, an address of an execution code and an address of a stack pointer at a temporary pause for a trace; and calculating and storing into a storage, a base address which is to be pointed by a base pointer by using the address of the execution code, the address of the stack pointer, and the table for the method including the execution code.
On the other hand, if a pause is taken and a trace is performed when a JITed last frame record mode (a mode wherein, if a first method compiled by a predetermined compiler has invoked a second method not compiled by a predetermined compiler, a record pointing to a stack frame of the first method is generated) is adopted in a suitable form and a non-JITed code is executed in a suitable form, the steps are taken for: obtaining and storing into a storage, an address of the stack frame of the first method referring to the record and an execution restarting address which maintained in the stack frame of the first method; and calculating and storing into a storage, a base address which is to be pointed by a base pointer by using the address of the stack frame of the first method, the execution restarting address and the table for the first method.
Thus, tracing a stack is possible even if optimization without the base pointer is performed. Accordingly, the above-mentioned problem of the language specifications of Java(trademark) is solved, and optimization without the base pointer can significantly improve the quality of a Java(trademark) compile code. On the other hand, in an environment where a mechanism of JITed code discarding is adopted, an efficient technique for retrieving an active method can be implemented without reduction in quality.
While the processing flow of the present invention was described as above, the present invention can also be implemented by a device for implementing these processes or a form of a program to have a computer implement these processes. It is normally the responsibility of one having ordinary skill in the art to store this program on storage media such as a floppy disk or a CD-ROM or any other form of storage media.